package edu.pku.se.chocolate.UI.Adapter;

import java.util.ArrayList;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import edu.pku.se.chocolate.ChocolateActivity;
import edu.pku.se.chocolate.UI.View.InformationView;
import edu.pku.se.chocolate.datastructure.Category;
import edu.pku.se.chocolate.datastructure.Tag;

public class TagRowAdapter extends BaseAdapter {
	
	private Category owner;
	private Context	mContext;
	private ArrayList<Tag> tags;
	private InformationView parentView;
	private int screenWidth = ChocolateActivity.DM.widthPixels;
	public TagRowAdapter (Category c,Context con,InformationView v) {
		owner = c;
		mContext = con;
		tags = ChocolateActivity.c_solver.getTagList(c);
		if(tags == null) {
			tags = new ArrayList<Tag>();
			tags.clear();
		}
		parentView = v;
//		tags = new ArrayList<Tag>();
//		tags.add(new Tag(0, null, "全部", null, null, 0, 1, 0,0));
//		tags.add(new Tag(0, null, "自行车", null, null, 0, 2, 0,0));
//		tags.add(new Tag(0, null, "手机", null, null, 0, 3, 0,1));
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		//循环
		//return Integer.MAX_VALUE;
		//非循环
		return tags.size();
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return 0;
	}
	
	public int select = 0;
	
	public void notifyDataSetChanged(int albumId) {
		select = albumId;
		parentView.setDeleteVisible((select!=0||owner.isCustomCategory())&&tags.size()>0);
		super.notifyDataSetChanged();
	}
	
	public void notifyDataSetChanged2(int albumId) {
//		select = albumId;
//		parentView.setDeleteVisible(select!=0||owner.isCustomCategory());
		super.notifyDataSetChanged();
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		//清空看过的标签的更新数
//		ChocolateActivity.c_solver.clearTagNewMsgNum(tags.get(parentView.getChoosedButton()));
//		tags.get(parentView.getChoosedButton()).new_msg_num = 0;
		if(tags.size()==0)
			return null;
		ImageView imageView = new ImageView(mContext);//初始化ImageView，用来显示各项
		if (select == position) {//当此项是选中项时，对起进行特殊处理
			Tag pro = tags.get(position % tags.size());//要实现循环播放，这里需要对数据大小进行求余
			if(pro.isNotify>0 && pro.new_msg_num != 0)
				imageView.setImageBitmap(BitmapUtil.createReflectedImage(BitmapUtil.createTxtImage(pro.name + "(" + pro.new_msg_num + ")",(40*screenWidth)/480)));//这里通过自定义的BitmapUtil类中的两个方法，实现了倒影效果
			else
				imageView.setImageBitmap(BitmapUtil.createReflectedImage(BitmapUtil.createTxtImage(pro.name,(40*screenWidth)/480)));//这里通过自定义的BitmapUtil类中的两个方法，实现了倒影效果
		} 
		else {
			Tag pro = tags.get(position % tags.size());
			if(pro.isNotify>0 && pro.new_msg_num != 0)
				imageView.setImageBitmap(BitmapUtil.createTxtImage(pro.name + "(" + pro.new_msg_num + ")",(30*screenWidth)/480));//这设置文字大小为22。实现了未选中项的变小效果
			else
				imageView.setImageBitmap(BitmapUtil.createTxtImage(pro.name,(30*screenWidth)/480));//这设置文字大小为22。实现了未选中项的变小效果
		}	
		return imageView;
	}
	
	public Tag getTag(int i) {
		if(tags.size()<=0)
			return null;
		return tags.get(i);
	}

	public void delete() {
		if(tags.size()>0) {
			Tag temp = tags.remove(select%tags.size());
			ChocolateActivity.c_solver.deleteTag(temp);
		}
		select = 0;
		parentView.setDeleteVisible((select!=0||owner.isCustomCategory())&&tags.size()>0);
	}
}
